import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { isH5 } from '@/utils/device'
import h5Routes from './h5'
import webRoutes from './web'
import { setupRouterGuard } from './permission'

// 公共路由
const commonRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '404',
      requiresAuth: false
    }
  },
  {
    path: '/403',
    name: 'Forbidden',
    component: () => import('@/views/error/403.vue'),
    meta: {
      title: '403',
      requiresAuth: false
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
];

// 根据设备类型选择路由
const routes = isH5() ? [...h5Routes, ...commonRoutes] : [...webRoutes, ...commonRoutes]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 设置路由守卫
setupRouterGuard(router)

export default router 